package com.gfxy.modules.educationandservice.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gfxy.modules.GfxyUtils;
import com.gfxy.modules.common.entity.TreeItemModel;
import com.gfxy.modules.common.service.IGfxyCommonService;
import com.gfxy.modules.common.service.IStaffTreeService;
import com.gfxy.modules.common.service.impl.GfxyCommonService;
import com.gfxy.modules.system.model.SysDepartTreeModel;
import org.apache.ibatis.annotations.Param;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import com.gfxy.modules.educationandservice.entity.EducationAndService;
import com.gfxy.modules.educationandservice.service.IEducationAndServiceService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 育人与服务
 * @Author: jeecg-boot
 * @Date:   2021-10-06
 * @Version: V1.0
 */
@Api(tags="育人与服务")
@RestController
@RequestMapping("/educationandservice/educationAndService")
@Slf4j
public class EducationAndServiceController extends JeecgController<EducationAndService, IEducationAndServiceService> {
	@Autowired
	private IEducationAndServiceService educationAndServiceService;

	@Autowired
	private IStaffTreeService staffTreeService;

	@Autowired
	private IGfxyCommonService gfxyCommonService;

     /**
	 * 分页列表查询
	 *
	 * @param educationAndService
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "育人与服务-分页列表查询")
	@ApiOperation(value="育人与服务-分页列表查询", notes="育人与服务-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(EducationAndService educationAndService,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<EducationAndService> queryWrapper = QueryGenerator.initQueryWrapper(educationAndService, req.getParameterMap());
//		Page<EducationAndService> page = new Page<EducationAndService>(pageNo, pageSize);
		List<EducationAndService> pageList = educationAndServiceService.list(queryWrapper);
		return Result.OK(pageList);
	}
//	 @AutoLog(value = "育人与服务-分页列表查询")
//	 @ApiOperation(value="育人与服务-分页列表查询", notes="育人与服务-分页列表查询")
//	 @GetMapping(value = "/list")
//	 public Result<?> queryPageList(EducationAndService educationAndService,
//									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//									HttpServletRequest req) {
//		 QueryWrapper<EducationAndService> queryWrapper = QueryGenerator.initQueryWrapper(educationAndService, req.getParameterMap());
//		 Page<EducationAndService> page = new Page<EducationAndService>(pageNo, pageSize);
//		 IPage<EducationAndService> pageList = educationAndServiceService.page(page, queryWrapper);
//		 return Result.OK(pageList);
//	 }

     /**
	 *   添加
	 *
	 * @param educationAndService
	 * @return
	 */
	@AutoLog(value = "育人与服务-添加")
	@ApiOperation(value="育人与服务-添加", notes="育人与服务-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody EducationAndService educationAndService) {
		educationAndServiceService.save(educationAndService);
		return Result.OK("添加成功！");
	}



	 @AutoLog(value = "育人与服务-保存")
	 @ApiOperation(value="育人与服务-保存", notes="育人与服务-保存")
	 @PutMapping(value = "/save")
	 public Result<?> save(@RequestBody JSONObject jsonObject) {

		 String workNo = jsonObject.getString("workNo");
		 String date = jsonObject.getString("date");
		 String orgCode = jsonObject.getString("orgCode");
		 JSONArray jsonArray = jsonObject.getJSONArray("items");
		 List<EducationAndService> educationAndServices = new ArrayList<>();
		 for (int i = 0; i < jsonArray.size(); i++) {
			 JSONObject item = jsonArray.getJSONObject(i);
			 EducationAndService educationAndService = new EducationAndService();
			 educationAndService.setDate(date);
			 educationAndService.setWorkNo(workNo);
			 educationAndService.setOrgCode(item.getString("orgCode"));
			 educationAndService.setProjectName(item.getString("projectName"));
			 educationAndService.setScore(item.getDouble("score"));
			 educationAndServices.add(educationAndService);
		 }

		 this.educationAndServiceService.saveAll(date,orgCode,workNo,educationAndServices);
		 return Result.OK("添加成功！");
	 }

	/**
	 *  编辑
	 *
	 * @param educationAndService
	 * @return
	 */
	@AutoLog(value = "育人与服务-编辑")
	@ApiOperation(value="育人与服务-编辑", notes="育人与服务-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody EducationAndService educationAndService) {
		educationAndServiceService.updateById(educationAndService);
		return Result.OK("编辑成功!");
	}

     /**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "育人与服务-通过id删除")
	@ApiOperation(value="育人与服务-通过id删除", notes="育人与服务-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		educationAndServiceService.removeById(id);
		return Result.OK("删除成功!");
	}

     /**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "育人与服务-批量删除")
	@ApiOperation(value="育人与服务-批量删除", notes="育人与服务-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.educationAndServiceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

     /**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "育人与服务-通过id查询")
	@ApiOperation(value="育人与服务-通过id查询", notes="育人与服务-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		EducationAndService educationAndService = educationAndServiceService.getById(id);
		if(educationAndService==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(educationAndService);
	}


	 @AutoLog(value = "育人与服务-查询总分")
	 @ApiOperation(value="育人与服务-查询总分", notes="育人与服务-查询总分")
	 @GetMapping(value = "/totalScore")
	 public Result<?> queryTotalScore(@RequestParam(name="date",required=true) String date,@RequestParam(name="workNo",required=true) String workNo) {
		 Map resMap = educationAndServiceService.queryTotalScore(date,workNo);
		 if(resMap==null || resMap.size()==0){
			JSONObject object = new JSONObject();
			object.put("totalScore",0);
		 	return Result.OK(object);
		 } else {
		 	return Result.OK(resMap);
		 }

	 }
    /**
    * 导出excel
    *
    * @param request
    * @param educationAndService
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, EducationAndService educationAndService) {
        return super.exportXls(request, educationAndService, EducationAndService.class, "育人与服务");
    }

	 /**
	  *
	  * @param req
	  * @return
	  */
	@AutoLog(value = "育人与服务-树部门或员工")
	@ApiOperation(value="育人与服务-树部门或员工", notes="育人与服务-树部门或员工")
	@GetMapping(value = "/educationTree")
	public Result<?> getEducationTree(HttpServletRequest req) {

		String rolesStr = GfxyUtils.getRoles(req);
		List<TreeItemModel> resultList = new ArrayList<>();
		if(this.isAllDataPermission(rolesStr)){
			resultList = this.gfxyCommonService.getTreeModelForOrg();
		} else {
			String orgCode = GfxyUtils.getOrgCode(req);
			resultList = this.gfxyCommonService.getTreeModelByOrgCode(orgCode);
		}
		return Result.OK(resultList);

	}


	 @AutoLog(value = "育人与服务-树部门或员工")
	 @ApiOperation(value="育人与服务-树部门或员工", notes="育人与服务-树部门或员工")
	 @GetMapping(value = "/educationTreeStaff")
	 public Result<?> getEducationTreeStaff(@RequestParam(name="orgCode",required=true) String orgCode, HttpServletRequest req) {


		 List<TreeItemModel> resultList = this.gfxyCommonService.getTreeModelTeachingTeacherByOrgCode(orgCode);
		 return Result.OK(resultList);

	 }

	 private boolean isAllDataPermission(String rolesStr) {
		if(rolesStr.contains("institute-operater")){
			return false;
		} else {
			return true;
		}
	 }


 }
